package tk.uodrad.apex.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import tk.uodrad.apex.project.mapper.PropertyMapper;
import tk.uodrad.apex.project.pojo.dto.PropertyAddNewDTO;
import tk.uodrad.apex.project.pojo.dto.PropertyUpdateDTO;
import tk.uodrad.apex.project.pojo.entity.Property;
import tk.uodrad.apex.project.pojo.vo.PropertyListVO;
import tk.uodrad.apex.project.service.PropertyService;
import tk.uodrad.apex.project.web.JsonResult;

import java.util.Arrays;


/**
 * 说明：
 */
@Slf4j
@RestController
@RequestMapping("/property")
@Transactional
public class PropertyController {
    @Autowired
    private PropertyService propertyService;
    @Autowired
    private PropertyMapper propertyMapper;
    /**
     * 添加房产
     * @param propertyAddNewDTO 添加房产DTO
     * */
    @PostMapping("/add")
    public JsonResult<String> add(@RequestBody PropertyAddNewDTO propertyAddNewDTO) {
        propertyService.save(propertyAddNewDTO);
        return JsonResult.ok("添加成功");
    }

    /**
     * 根据ID数组删除房产
     * @param ids ID数组
     * */
    @GetMapping("/delete")
    public JsonResult<String> deleteById(Long[] ids) {
        propertyService.removeByIds(Arrays.asList(ids));
        return JsonResult.ok("删除成功！");
    }

    /**
     * 根据ID删除房产
     * @param id 房产ID
     * */
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> delete(@PathVariable @Range(min = 1, message = "请提交有效的ID值！") Long id){
        propertyService.removeById(id);
        return JsonResult.ok();
    }

    /**
     * 根据ID修改房产信息
     * @param propertyUpdateDTO 修改房产DTO
     * */
    @PostMapping("/update")
    public JsonResult<String> update(@RequestBody PropertyUpdateDTO propertyUpdateDTO) {
        propertyService.updateById(propertyUpdateDTO);
        return JsonResult.ok("修改成功！");
    }

    /**
     * 分页查询成员列表
     * @param pageNum 第几页
     * @param pageSize 每页多少条数据
     * @param communityCode 小区编号
     * @return IPage<PropertyListVO> PropertyListVO分页对象
     */
    @GetMapping("/list")
    public JsonResult<IPage<PropertyListVO>> list(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                  @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                  @RequestParam(value = "communityCode", required = false) String communityCode) {
        IPage<PropertyListVO> list = propertyService.list(pageNum, pageSize,communityCode);
        return JsonResult.ok(list);
    }

    /**
     * 根据房产id查询详情
     * */
    @GetMapping("/{id:[0-9]+}")
    public JsonResult<Property> getListById(
            @PathVariable @Range(min = 1, message = "请提交有效的ID值！") Long id){
        Property queryResult = propertyMapper.selectById(id);
        return JsonResult.ok(queryResult);
    }
}
